import request from '@/common/request'
import store from '@/store'
import * as url from "./url"

// 记录登录信息
function rememberLogin (res, phone) { 
  if (res.errorCode === '0000') {
    const loginInfo = {...res.data[1],...res.data[0]}
    store.commit('mLogin', loginInfo);
    window.localStorage.setItem("token",loginInfo.token);
    window.localStorage.setItem("tokenStartTime",+new Date());
  }
}

// 获取注册验证码
export function postUserCode (phone) {
  
  return postRequest({type:"code",data:{phone}})
  // return request('post', '/api/user/code', { phone })
}

// 使用手机号注册
export function postUserRegister (phone, password, vcode) {
  return postRequest({type:"register",data:{phone, password, vcode }})
  // return request('post', '/api/user/register', { })
}

// 密码登录接口
export function postUserLogin (phone, password) {
  return postRequest({type:"login",data:{phone, password }}).then(res => {
    rememberLogin(res, phone)
    return res
  })
  // return request('post', '/user/api/p/login', { phone, password }) 
}
// 密码登录接口
export function wxLogin (token) {
  window.localStorage.setItem("token",token);
  window.localStorage.setItem("tokenStartTime",+new Date());
  return postRequest({type:"getUserByToken"}).then(res => {
    if (res.errorCode === '0000') {
      const loginInfo = res.data;
      loginInfo.token=token;
      loginInfo.expTime=7200;
      loginInfo.genTime=+new Date();
      store.commit('mLogin', loginInfo); 
    }
    return res
  })
  // return request('post', '/user/api/p/login', { phone, password }) 
}

// 获取登录验证码
export function postUserMsg (phone) {
  return postRequest({type:"msg",data:{phone}})
  // return request('post', '/api/user/msg', { phone })
}

// 短信登录接口
export function postUserLoginForMsg (phone, vcode) {
  return postRequest({type:"msglogin",data:{ phone, vcode}}).then(res => {
    rememberLogin(res, phone)
    return res
  })
  // return request('post', '/api/user/msg/login', { phone, vcode })
}

// 获取首页商品分类导航
export function postIndexNav () {
  return postRequest({type:"queryAllType"})
  // return request('post', '/item/api/p/index/queryAllType')
}

// 获取首页横向导航
export function postIndexLineNav () {
  return postRequest({type:"queryTransverse" })
  // return request('post', '/item/api/p/index/queryTransverse')
}

// 获取轮播图
export function postIndexCarousel () {
  return postRequest({type:"queryBanner" })
  // return request('post', '/item/api/p/index/queryBanner')
}

// 获取今日推荐
export function postIndexRecommend () {
  return postRequest({type:"queryTodayRecommend"})
  // return request('post', '/item/api/p/index/queryTodayRecommend')
}

// 获取即将开售
export function postIndexSell () {
  return postRequest({type:"queryToSaleItem" })
  // return request('post', '/item/api/p/index/queryToSaleItem')
}

// 获取楼层数据
export function postFloorData () {
  return postRequest({type:"queryFloorItems"})
  // return request('post', '/item/api/p/index/queryFloorItems')
}

// 获取分类排行
export function postIndexSeniority (itemTypeId) {
  return postRequest({type:"queryHotItems",data:{itemTypeId}})
  // return request('post', '/item/api/p/index/queryHotItems', { itemTypeId })
}



/*
通过postRequest 请求数据
参数params格式为：
      {
        type:"string", //type请求变量的url地址常量名称，参考url.js文件里面的常量.
        data:{object}  //data传递到后台的参数信息，主要是object对象，传入时进行对象转换
      }
*/
export function postRequest (params) {
  return request('post', url[params.type], {...params.data}).then(res=>{
    changeToken();
    return res;
  },err=>{
    changeToken();
  })
}
export function restGetRequest (params) {
  return request('rest', url[params.type], {...params.data}).then(res=>{
    changeToken();
    return res;
  },err=>{
    changeToken();
  })
}
//  是否已经发送Token更新请求，如果是为true，否则为false；
let isSendToken=false;
function changeToken(){
  let oldTokenTime=parseInt(window.localStorage.getItem("tokenStartTime"));
  let newTime=+new Date();
  //  100分钟后重新更新Token对象 *60*100 
  if(newTime-oldTokenTime>1000*60*100  && !isSendToken){
    isSendToken=true;
    postRequest({
      type:"replaceToken",
      data:{token:window.localStorage.getItem("token")}
    }).then(res=>{
      if(!(res&&res.data)){return}
      let tokenData = res.data;
      window.localStorage.setItem("token",tokenData.token);
      window.localStorage.setItem("tokenStartTime",+new Date());
      isSendToken=false;
    }) 

  }
}
/* 测试示例 */
export function indexWebTopNav () {
  return request('post', '/api/index/webtopnav')
}
export function  getUrlParam (name) {
  var reg = new RegExp(name + "=([^&]*)(&|$)");
  var r = window.location.hash.match(reg);
  if (r != null) return unescape(r[1]); return null;
}
export function  getCookie (name) {
  var reg = new RegExp(name + "=([^&]*)(&|$)");
  var r = window.location.hash.match(reg);
  if (r != null) return unescape(r[1]); return null;
}


